﻿using System;
using System.IO;

namespace SmartWMS.Common.Jwt
{
    public class JwtSecret
    {
        /// <summary>
        /// 读取txt文件，token私钥(随机字符串)
        /// 秘钥长度必须：>=16位
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetAccountKey()
        {
            //设置[公钥私钥]文件路径
            string privateKeyPath = @"privateKey.txt";
            var privateKey = GetSecurityKey(privateKeyPath);
            return privateKey;
        }

        /// <summary>
        /// 读取txt文件，刷新私钥(随机字符串) 
        /// 秘钥长度必须：>=16位
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetRefreshKey()
        {
            //设置[公钥私钥]文件路径
            string refreshKeyPath = @"refreshKey.txt";
            var refreshKey = GetSecurityKey(refreshKeyPath);
            return refreshKey;
        }

        /// <summary>
        /// 秘钥长度必须：>=16位
        /// </summary>
        /// <param name="tmpPath"></param>
        /// <returns></returns>
        private static string GetSecurityKey(string tmpPath)
        {
            //设置[公钥私钥]文件路径
            string fullName = Environment.CurrentDirectory;
            var fullPath = fullName.Substring(0, fullName.LastIndexOf("\\")) + "\\SmartWMS\\wwwroot\\jwtkey\\" + tmpPath;
            var securityKey = File.ReadAllText(fullPath);
            return securityKey;
        }
    }
}
